﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DigitalAnts.webUserControl
{
    public partial class MainHeader : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pageLoad();
        }

        public void pageLoad()
        {
            lbLoginStatus.Text = "Đăng Nhập";

            //Count product from user's cart
            if (HttpContext.Current.Session["Cart"] != null)
            {
                List<int> data = (List<int>)Session["Cart"];
                int num = data.Count;
                if (num != 0)
                {
                    this.hpCart.Text = "Giỏ hàng <strong>(" + num + ")</strong>";
                }
            }
            if (HttpContext.Current.Session["Logged"] != null)
            {
                if (HttpContext.Current.Session["Logged"].Equals(true))
                {
                    lbLoginStatus.Text = "Thoát";
                    hpRegister.Text = "Danh sách yêu thích";
                    hpRegister.NavigateUrl = "~/User/FavoriteList.aspx";
                    hpUser.NavigateUrl = "~/User/UserPage.aspx";
                    hpUser.Text = " Tài khoản <strong>(" + Session["UserName"] + ")</strong>";
                    //Count product from user's favorite list
                    if (HttpContext.Current.Session["Favorite"].ToString() != "")
                    {
                        List<int> fdata = (List<int>)Session["Favorite"];
                        int fnum = fdata.Count;
                        this.hpRegister.Text = "Danh sách yêu thích <strong>(" + fnum + ")</strong>";
                    }
                }
            }
            
        }

        protected void lbLogin_Click(object sender, EventArgs e)
        {   
            //Logout
            if (lbLoginStatus.Text.Equals("Thoát"))
            {
                HttpContext.Current.Session["Logged"] = false;
                HttpContext.Current.Session["Role"] = "Guest";
                HttpContext.Current.Session["Cart"] = "";
                HttpContext.Current.Session["Favorite"] = "";
                HttpContext.Current.Session.Remove("UserID");
                HttpContext.Current.Session.Remove("UserName");
            }
            Response.Redirect("~/User/UserLogin.aspx");
        }
    }
}